package com.py.credit.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.py.credit.dao.DataChooseTableDao;
import com.py.credit.service.DataChooseTableService;
import com.py.framework.core.common.Page;

@Service
public class DataChooseTableServiceImpl implements DataChooseTableService {
	// 工商基础信息表
	private static final String REGISTER_TABLE_NAME = "t_100000460009_jnuqj847ju";
	// 法人行政处罚
	private static final String FR_PUNISH = "t_1000000010002_XSni4zSzP0";
	// 法人行政许可
	private static final String FR_PERMIT = "t_1000000010001_3ioj4GeZQU";
	// 法人黑名单
	private static final String FR_BLACKMENU = "t_1000000010007_u6tnVOcB9I";
	// 法人红名单
	private static final String FR_REDMENU = "t_1000000010005_JgddEske8s";
	@Autowired
	private DataChooseTableDao dataChooseTableDao;

	@Override
	public List<Map<String, Object>> DataChooseOneTableForPage(String input, String code, Page page) {
		int i = 0;
		List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
		String tableName = REGISTER_TABLE_NAME;
		
		String reg = "[\\u4e00-\\u9fa5]+";
		if (input.matches(reg)) {
			mapList = dataChooseTableDao.DataChooseOneTablesForPage(input, tableName, page);
		} else {
			mapList = dataChooseTableDao.DataChooseOneTableForPage(input, tableName, page);
		}
		return mapList;
	}

	@Override
	public Map<String, Integer> selectAllDataCount() {
		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("GJCount", dataChooseTableDao.selectGjCount());
		map.put("GJMenUCount", dataChooseTableDao.selectMenuCount());
		return map;
	}

	@Override
	public List<Map<Object, Object>> selectEvMenCount() {
		List<Map<Object, Object>> list = dataChooseTableDao.selectEvMenCount();
		Map<Object, Object> m = new HashMap<Object, Object>();
		Map<Object, Object> madd = new HashMap<Object, Object>();
		Map<String, Integer> map = this.selectAllDataCount();
		int j = 0;
		int a = 0;
		int c = 0;
		BigDecimal big = null;
		if (!CollectionUtils.isEmpty(list)) {
			for (int i = 0; i < list.size(); i++) {
				m = list.get(i);
				if (!CollectionUtils.isEmpty(m) && m.containsKey("sum")) {
					big = (BigDecimal) m.get("sum");
					c = big.intValue();
					j = j + c;
				}
			}
		}
		if (!CollectionUtils.isEmpty(map)) {
			if (map.get("GJCount") == null) {
				a = 0;
			} else {
				a = (Integer) map.get("GJCount");
			}
		}
		madd.put("NAME", "其它");
		madd.put("sum", a - j);
		list.add(madd);
		return list;
	}

	@Override
	public List<Map<Object, Object>> selectDeptDataCountForPage(Page page) {
		List<Map<Object, Object>> listMap = dataChooseTableDao.selectDeptDataCountForPage(page);
		return listMap;
	}

	@Override
	public Map<Object, Integer> selectAllAndMonthCount() {
		Map<Object, Integer> map = new HashMap<Object, Integer>();
		map.put("GJCount", dataChooseTableDao.selectGjCount());
		map.put("MouthGJCount", dataChooseTableDao.selectMonthCount());
		return map;
	}

	@Override
	public List<Map<Object, Object>> selectGJEntityCount() {
		List<Map<Object, Object>> list = dataChooseTableDao.selectGJEntityCount();
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("GJCount", dataChooseTableDao.selectGjCount());
		list.add(map);
		return list;
	}

	@Override
	public List<Map<Object, Object>> selectPSACount() {
		List<Map<Object, Object>> list = dataChooseTableDao.selectPSACount();
		Map<Object, Object> map = new HashMap<Object, Object>();
		map.put("GJCount", dataChooseTableDao.selectGjCount());
		list.add(map);
		return list;
	}

	@Override
	public List<Map<Object, Object>> selectDeptCount() {
		List<Map<Object, Object>> list = dataChooseTableDao.selectDeptCount();
		return list;
	}
}
